package cn.piesat.scanning.business.dqenvironment.algorithm;

import cn.piesat.scanning.business.common.algorithm.dataScan.DataScanAbstract;
import cn.piesat.scanning.business.common.algorithm.dataScan.DataScanParams;
import cn.piesat.scanning.dto.DbDmsScanningMetadataAuxInfoDTO;
import cn.piesat.scanning.dto.HtPathConfigurationDTO;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.*;

/**
 * 数据入库插件
 *
 * @author xhl
 */
@Component
public class DataScan extends DataScanAbstract {

    private static final Logger logger = LoggerFactory.getLogger(DataScan.class);

    @Override
    public void assembleAux(List<Object> pojoList, HtPathConfigurationDTO pathDTO, File file,
                            Set<String> existFileNameSet, String auxType) {
        if (auxType.equals("ECMWF_C1D_SURF")){
            DbDmsScanningMetadataAuxInfoDTO auxInfoDTO = new DbDmsScanningMetadataAuxInfoDTO();
            String fileName = file.getName();
            if (existFileNameSet.contains(fileName)) {
                return;
            }
            //C1D_SURF_2021013112_2021021010.nc
            // 0    1   2            3
            String[] split = fileName.split("_");
            auxInfoDTO.setFileDate(split[2]+"0000");
            String forecastDate = split[3].split("\\.")[0]+"0000";
            auxInfoDTO.setForecastDate(forecastDate);
            String filePath = file.toString().substring(pathDTO.getScanningPath().length());
            auxInfoDTO.setFileName(fileName);
            auxInfoDTO.setFilePath(filePath);
            auxInfoDTO.setFileSize(file.length());
            auxInfoDTO.setFileCatalog(auxType);
            pojoList.add(auxInfoDTO);
        }
    }

    @Override
    public void assembleDist(List<Object> pojoList, HtPathConfigurationDTO pathDTO, File file, Set<String> existFileNameSet, DataScanParams dataScanParams) {
        HtScanningMetadataDTO metadataDTO = new HtScanningMetadataDTO();
        String fileName = file.getName();
        String path = file.toString().substring(pathDTO.getScanningPath().length());
        if (existFileNameSet.contains(fileName)) {
            return;
        }
        splitFileName(fileName,metadataDTO);
        metadataDTO.setFilePath(path);
        metadataDTO.setFileName(fileName);
        metadataDTO.setFileSize(file.length());
        metadataDTO.setPathConfigId(pathDTO.getId());
        metadataDTO.setFileType(fileName.substring(fileName.lastIndexOf(".")+1).toUpperCase());
        metadataDTO.setCreateTime(new Date());
        metadataDTO.setFileCatalog(dataScanParams.getAuxType());
        pojoList.add(metadataDTO);
    }

    @Override
    public void kafkaSave(String taskId, HtPathConfigurationDTO pathDTO) {

    }

    /**
     * 获取文件名称中的 卫星、传感器、分辨率、等级、日期等信息
     * PS：因各个卫星命名规则不同，当接入新的数据时需增加对应文件名称 [格式分割代码]
     * @param fileName 文件名称
     * @param entityDto 实体类
     */
    private void splitFileName(String fileName,HtScanningMetadataDTO entityDto){
        String[] nameSplit = fileName.split("_");
        String fileDate = "";
        if (fileName.contains("HS_H08")){
            //HS_H08_20210322_0050_B01_FLDK_R10_S0110.DAT.bz2
            // 0  1    2       3    4   5    6   7
            entityDto.setSatellite(nameSplit[1]);
            fileDate = nameSplit[2]+nameSplit[3];
            entityDto.setBands(nameSplit[4]);
            entityDto.setResolution(nameSplit[6].replace("R",""));
            entityDto.setDataLevel("L0");

        }else if(fileName.contains("SURF_CHN_MUL_HOR-all")){
            //SURF_CHN_MUL_HOR-all-2021030900.txt
            // 0    1   2   3       4
            String str = nameSplit[3];
            String fd = str.split("-")[2];
            fileDate = fd.substring(0,fd.lastIndexOf(".txt"));
            entityDto.setDataLevel("L0");
        }else if(fileName.contains(".json")){
            //2021020414.json
            fileDate = fileName.substring(0,fileName.lastIndexOf(".json"));
            entityDto.setDataLevel("L0");
        }
        //做fileDate时间补齐
        StringBuffer sb = new StringBuffer();
        if(fileDate.length()<14){
           int num = 14-fileDate.length();
           for(int i=0;i<num;i++){
               sb.append("0");
           }
           fileDate = fileDate +sb;
        }
        entityDto.setFileDate(fileDate);
    }



}
